import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    levelActive: sessionStorage.getItem('levelActive') || "0",
    isHidden: sessionStorage.getItem('isHidden') ? JSON.parse(sessionStorage.getItem('isHidden')) : false,
    weather: sessionStorage.getItem('weather') ? JSON.parse(sessionStorage.getItem('weather')) : false,
    loginInfo: JSON.parse(localStorage.getItem("loginInfo"))
      ? JSON.parse(localStorage.getItem("loginInfo"))
      : {
        saveType: false,
        name: "",
        pwd: "",
      },
    edouardSDKToken: localStorage.getItem("edouardSDKToken") || "",
    userData: JSON.parse(localStorage.getItem("userData"))
      ? JSON.parse(localStorage.getItem("userData"))
      : {},
    proData: JSON.parse(localStorage.getItem("proData"))
      ? JSON.parse(localStorage.getItem("proData"))
      : {},
  },
  getters: {
    levelActive: state => state.levelActive,
    isHidden: state => state.isHidden,
    weather: state => state.weather,
    loginInfo: (state) => state.loginInfo,
    edouardSDKToken: (state) => state.edouardSDKToken,
    userData: (state) => state.userData,
    proData: (state) => state.proData,
  },
  mutations: {
    SETlevelActive(state, data) {
      console.log(data);
      sessionStorage.setItem('levelActive', data);
      state.levelActive = data
    },
    SETisHidden(state, data) {
      console.log(data, typeof data);
      sessionStorage.setItem('isHidden', data);
      state.isHidden = data
    },
    SETweather(state, data) {
      console.log(data, typeof data);
      sessionStorage.setItem('weather', data);
      state.weather = data
    },
    setLoginInfo(state, data) {
      localStorage.setItem("loginInfo", JSON.stringify(data));
      state.loginInfo = data;
    },
    setToken(state, data) {
      localStorage.setItem("edouardSDKToken", data);
      state.edouardSDKToken = data;
    },
    setUserData(state, data) {
      localStorage.setItem("userData", JSON.stringify(data));
      state.userData = data;
    },
    setProData(state, data) {
      localStorage.setItem("proData", JSON.stringify(data));
      state.proData = data;
    },
  },
  actions: {
    setlevelActive({
      commit
    }, data) {
      console.log(data);
      commit("SETlevelActive", data)
    },
    setisHidden({
      commit
    }, data) {
      console.log(data);
      commit("SETisHidden", JSON.stringify(data))
    },
    setweather({
      commit
    }, data) {
      console.log(data);
      commit("SETweather", JSON.stringify(data))
    },
    setLoginInfo({
      commit
    }, data) {
      commit("setLoginInfo", data)
    },
    setToken({
      commit
    }, data) {
      commit("setToken", data)
    },
    setUserData({
      commit
    }, data) {
      commit("setUserData", data)
    },
    setProData({
      commit
    }, data) {
      commit("setProData", data)
    },
  },
  modules: {
  }
})
